package com.example.boot.service.simple_test.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.boot.domain.Payment;
import com.example.boot.mapper.simple_test.PaymentMapper;
import com.example.boot.service.simple_test.IOrderService;
import com.example.boot.service.simple_test.IPaymentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

/**
*
*/
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements IPaymentService {

    @Resource
    private PaymentMapper paymentMapper;
    @Resource
    private IOrderService iOrderService;

    @Override
    public void executeAsync() {
        Runnable rn = ()->{
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("------------PaymentServiceImpl end-----------");
        };
        new Thread(rn).start();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testUpdate(String str) {
        System.out.println(Thread.currentThread().getName()+"============start==========");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Payment payment = new Payment();
        payment.setId((long) 10002);
        payment.setSerial(str);
        paymentMapper.updateById(payment);
        System.out.println(Thread.currentThread().getName()+"============end===========");
    }

    @Override
    public void testAsyn() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("================");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTransaction() {
        Payment payment = paymentMapper.selectById(1001);
        int i = Integer.parseInt(payment.getSerial());
        payment.setSerial(String.valueOf(i+1));

        paymentMapper.updateById(payment);
        try {
            payment.setSerial(String.valueOf(i+5));
            paymentMapper.updateById(payment);
            int i1 = 1 / 0;
        } catch (Exception e) {
            // throw new RuntimeException("11");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        /*if (true){
            throw new RuntimeException("11");
        }*/
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTransaction1() {
        Payment payment = paymentMapper.selectById(1001);
        int i = Integer.parseInt(payment.getSerial());
        payment.setSerial(String.valueOf(i+1));
        paymentMapper.updateById(payment);

        try {
            doAnaotherTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            // throw new RuntimeException("222");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        System.out.println("======");
    }


    @Transactional(rollbackFor = Exception.class)
    public void doAnaotherTransaction() {
        Payment payment = paymentMapper.selectById(1001);
        payment.setSerial("5000");
        paymentMapper.updateById(payment);
        int i = 1 / 0;
    }

    @Override
    public void funcA() {
        iOrderService.funB();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTran1() {
        try {
            iOrderService.testTran1();
            iOrderService.testTran2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
